package com.zhouzhou.fastjava;

import org.slf4j.helpers.MessageFormatter;

public class BizException extends RuntimeException {

    private final String code;

    public BizException(String code, String msg, Throwable cause) {
        super(msg, cause);
        this.code = code;
    }

    public BizException(Throwable cause) {
        this("ERROR", cause.getMessage(), cause);
    }

    public String getCode() {
        return code;
    }

    public static BizException of(String code, Throwable cause, String pattern, Object... args) {
        return new BizException(code, MessageFormatter.arrayFormat(pattern, args).getMessage(), cause);
    }

    public static BizException codeOf(String code, String pattern, Object... args) {
        return of(code, null, pattern, args);
    }

    public static BizException messageOf(String pattern, Object... args) {
        return of("ERROR", null, pattern, args);
    }

    public static BizException rethrow(Throwable e) {
        return new BizException(e);
    }

}
